﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GridViewCustomBinding.Model {
    public class Car {
        public int id { get; set; }
        public Engine engine { get; set; }
        public string model { get; set; }
        public string brand { get; set; }
    }

    public class Engine {
        public int id { get; set; }
        public string brand { get; set; }
        public string model { get; set; }
        public int horsePower { get; set; }
    }

    public static class GridModel {
        const string ModelKey = "ModelKey";

        public static IEnumerable<Car> DB {
            get {
                if (HttpContext.Current.Items[ModelKey] == null)
                    HttpContext.Current.Items[ModelKey] = GenerateModel();
                return (IEnumerable<Car>)HttpContext.Current.Items[ModelKey];
            }
        }
        
        static IEnumerable<Car> GenerateModel() {
            IList<Engine> engines = GenerateEngine();
            return new List<Car>() {
                new Car() { id = 1, engine = engines[0], brand = "brand 1", model = "model 1" },
                new Car() { id = 2, engine = engines[1], brand = "brand 2", model = "model 2" },
                new Car() { id = 3, engine = engines[2], brand = "brand 3", model = "model 3" },
                new Car() { id = 4, engine = engines[0], brand = "brand 4", model = "model 4" },
                new Car() { id = 5, engine = engines[1], brand = "brand 5", model = "model 5" },
                new Car() { id = 6, engine = engines[2], brand = "brand 6", model = "model 6" },
                new Car() { id = 7, engine = engines[0], brand = "brand 7", model = "model 7" },
                new Car() { id = 8, engine = engines[1], brand = "brand 8", model = "model 8" },
                new Car() { id = 9, engine = engines[2], brand = "brand 9", model = "model 9" },
                new Car() { id = 10, engine = engines[0], brand = "brand 10", model = "model 10" },
                new Car() { id = 11, engine = engines[1], brand = "brand 11", model = "model 11" },
                new Car() { id = 12, engine = engines[2], brand = "brand 12", model = "model 12" },
            };
        }
        static IList<Engine> GenerateEngine() {
            return new List<Engine>() {
                new Engine() { id =  0, brand = "engine brand 1", horsePower = 100, model = "engine mode 1" },
                new Engine() { id =  1, brand = "engine brand 2", horsePower = 200, model = "engine mode 2" },
                new Engine() { id =  4, brand = "engine brand 3", horsePower = 100, model = "engine mode 3" }
            };
        }
    }
}